package com.san.app.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.san.app.dao.InspectTemplateDao;
import com.san.app.domain.InspectTemplateDTO;
import com.san.app.service.InspectTemplateService;
import com.san.app.vo.InspectTemplateVO;
import com.san.common.utils.Constant;
import com.san.metadata.dao.ManageMetadataDao;
/**
 * 信用核查模板Service实现类
 * @author SuperSR
 * @date 2019年3月27日 下午2:43:49
 *
 */
@Service
public class InspectTemplateServiceImpl implements InspectTemplateService{

	@Autowired
	private InspectTemplateDao dao;
	@Autowired
	private ManageMetadataDao manageMetadataDao;
	
	@Override
	public InspectTemplateDTO getById(Long id) {
		return dao.getById(id);
	}
	
	@Override
	public List<InspectTemplateDTO> getTemplateList(Map<String, Object> map) {
		return dao.getTemplateList(map);
	}

	@Override
	public int save(InspectTemplateVO inspectTemplateVO) {
		getTemplateSql(inspectTemplateVO);
		return dao.save(inspectTemplateVO);
	}

	/**
	 * 获取templateSql
	 * @param inspectTemplateVO
	 */
	private void getTemplateSql(InspectTemplateVO inspectTemplateVO) {
		int inspectType = inspectTemplateVO.getInspectType();
		StringBuilder sb = new StringBuilder("select " + inspectTemplateVO.getColumnName() + " from " + manageMetadataDao.getById(inspectTemplateVO.getTableId()).getTargetTableName() + " where ");
		if(inspectType == Constant.MANAGE_DATA_TYPE_LEGAL_PERSON){
			sb.append("legal_person_id = ?");
		} else if(inspectType == Constant.MANAGE_DATA_TYPE_NATURAL_PERSON){
			sb.append("natural_person_id = ?");
		} /*else if(inspectType == Constant.MANAGE_DATA_TYPE_LEGAL_PERSON_OR_NATURAL_PERSON  || inspectType == Constant.MANAGE_DATA_TYPE_LEGAL_PERSON_AND_NATURAL_PERSON || inspectType == Constant.MANAGE_DATA_TYPE_NATURAL_PERSON_OR_NATURAL_PERSON){
			sb.append("legal_person_id = ? or natural_person_id = ?");
		} else if(inspectType == Constant.MANAGE_DATA_TYPE_LEGAL_PERSON_AND_NATURAL_PERSON){
			sb.append("legal_person_id = ?  or natural_person_id = ? ");
		} else if(inspectType == Constant.MANAGE_DATA_TYPE_NATURAL_PERSON_OR_NATURAL_PERSON){
			sb.append("natural_person_id = ? or natural_person_id1 = ? ");
		}*/
		inspectTemplateVO.setTemplateSql(sb.toString());
	}

	@Override
	public int doEdit(InspectTemplateVO inspectTemplateVO) {
		getTemplateSql(inspectTemplateVO);
		return dao.doEdit(inspectTemplateVO);
	}

	@Override
	public int remove(Long id) {
		return dao.remove(id);
	}

}
